home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 552 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.7 KB  |  102 lines

  1. Path: fnpx20.fnal.gov!not-for-mail
  2. From: sfield@fnpx20.fnal.gov (Stephen Field)
  3. Newsgroups: comp.lang.c
  4. Subject: Q: Terminating program at EOF
  5. Date: 6 Jan 1996 19:04:19 -0600
  6. Organization: FERMILAB, Batavia, IL
  7. Message-ID: <4cn66j$5r0@fnpx20.fnal.gov>
  8. NNTP-Posting-Host: fnpx20.fnal.gov
  9. Summary: help
  10. X-Newsreader: NN version 6.5.0 #4 (NOV)
  11.  
  12. Hi,
  13.  
  14. I'm writing a program that looks at poorly formatted text files and reformats
  15. them to a user-specified line length. The input text file has a blank line
  16. between paragraphs and I would like the newly formatted file to also have blank
  17. lines between paragraphs.
  18.  
  19. I've written a program that does the job, but it doesn't stop when it reaches
  20. the end of the file. The program will write out the reformatted file, but it
  21. appends a lot of "extra" characters to the end of the file and only stops when
  22. I break out of it.
  23.  
  24. I have tried this program on many files and it behaves the same. The program
  25. is included below and I've got it running on an SGI running IRIX.
  26.  
  27. Any help appreciated.
  28.  
  29. Steve
  30.  
  31. ===================================================
  32. /*    This program reformats a file that is input by the user to not exceed
  33.     a number of columns that is also input and outputs the reformatted file
  34.     to a file also given by the user at runtime. Lines are broken at spaces. */
  35. #include <stdio.h>
  36. #define MAX_WORD_LENGTH 80    /* max length of word in input */
  37.  
  38. main()
  39. {
  40.   int line_length,        /* max length of line in output */
  41.       curr_line_length = 0,    /* # of chars printed so far in curr line */
  42.       word_length;        /* length of current word */
  43.   char word[ MAX_WORD_LENGTH+1];/* buffer to read word +1 for terminating
  44.                     '\0' */
  45.   char c_old=' ',c_new=' ';     /* check for newlines with chars */
  46.   FILE *fptr_read,*fptr_write;  /* file to write to */
  47.  
  48.   char translate[3]={'\'','\"','\"'}; /* translate "wierd" chars to ASCII */
  49.  
  50.   printf("Enter file name to read: "); /* Get the input file */
  51.   scanf("%s",word);
  52.   fptr_read=fopen(word,"r");    /* open the file, don't check */
  53.  
  54.   printf("Enter line length: ");
  55.   scanf("%d",&line_length);    /* input new line length */
  56.  
  57.   printf("Enter file name to output to: ");
  58.   scanf("%s",word);             /* file name to output to */
  59.   fptr_write=fopen(word,"w");
  60.  
  61. /*  while((c_new=fgetc(fptr_read))!=EOF){ */ /* old method for removing EOF bug */
  62.     while(c_new!=EOF){                       /* new method, still doesn't work */
  63.       c_new=fgetc(fptr_read);
  64.     if(c_new=='\n'){             /* if fgetc sees a \n, there's 2 \n's in a row */
  65.       fprintf(fptr_write,"\n");  /* make sure paragraphs are sep. by a line */
  66.       curr_line_length=0;        /* reset the line length */
  67.       continue;                  /* get the next char */
  68.     }
  69.     else {
  70.       ungetc(c_new,fptr_read);   /* it's not a \n, so put it back for fscanf */
  71.     }
  72.     if(fscanf(fptr_read,"%s",word)==EOF) /* try to fix EOF bug here */
  73.     break;
  74.     /* determine length of current word */
  75.     for(word_length = 0 ; word[word_length]!='\0';
  76.     word_length++)
  77.       word[word_length]=word[word_length]>=146? /* translate wierd chars here */
  78.     translate[word[word_length]-146]:word[word_length];
  79.  
  80.     if(word_length>line_length)
  81.       printf("\nERROR: word length exceeds line length!!");
  82.     if(curr_line_length==0){ /* first word to write? */
  83.       fprintf(fptr_write,"%s",word);
  84.       curr_line_length=word_length;
  85.     }
  86.     else {
  87.       /* +1 is for space */
  88.       curr_line_length += word_length + 1;
  89.       /* if word won't fit on current line,
  90.      start a new line */
  91.       if(curr_line_length>line_length){
  92.     fprintf(fptr_write,"\n%s",word);
  93.     curr_line_length=word_length;
  94.       }
  95.       else
  96.         fprintf(fptr_write," %s",word); /* word fits, add space and 
  97.                                            word to current line */
  98.     }
  99.   }
  100. }
  101.  
  102.